How to drop a user in SQL Server?
How to drop a user in SQL Server?
429
16-Jul-2024
Updated on 16-Jul-2024
Ashutosh Kumar Verma
16-Jul-2024SQL Server DROP USER
The SQL statement
DROP USERallows you to delete a user from an existing database. Here is the syntax of theDROP USERstatementIn the syntax above, don't forget to replace your use name with the
user_nameafter the keyworkDROP USER. If the user doesn't exist in the current database thenDROP USERstatement will fail.To avoid this, you can use the
IF EXISTSoption. If the user already exists, theIF EXISTSoption conditionally deletes them.1. Using the DROP USER statement to delete a user
Now, use the
MyCollegeDbsample database for the followingDROP USERstatement example.First, create a new login
testLoginwith a password,Second, create a new user and map it with the login
testLoginThird, drop the user
MyUserfrom the current database2. Drop a user that owns a securable
First, create a new login called
jacobwith a password,Second, use your current database
MyCollegeDband create a new user for the loginjacobThird, create a schema called
reportand grant authorization to the userami.Fourth, connect to the SQL Server using the login
jacoband create a table calleddaily_salesin the schemareportFifth, switch the connection to the system administrator (
sa) account and drop the useramiThe SQL server Returns the following error,
Because the user
amiowns the schemareport, theDROP USERstatement cannot delete it.To delete user
ami, you must first transfer the authorization of the schema report to another user. For example, the following statement changes the authorization of the schemareportto userdbo:If you execute the
DROP USERstatement to delete the userami, it will executes successfully,Also, Read: Describe the different types roles in SQL Server.